package rmi;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;

import po.UserIdentity;

import bl.controller.*;
import bl.service.*;

import vo.BookVO;
import vo.CatalogVO;
import vo.CustomerBasicInfoVO;
import vo.OrderVO;
import vo.SaleItemVO;
import vo.SearchKeyVO;
import vo.BLToViewResultMessage;
import exception.*;

public class CustomerServiceRemoteImpl extends UnicastRemoteObject implements
		CustomerServiceRemote {

	private static final long serialVersionUID = 279269289547177756L;

	public CustomerServiceRemoteImpl() throws RemoteException {
		super();
	}

	@Override
	public BLToViewResultMessage requireRegister(String username,
			String password) throws RemoteException {
		AccountBLService service = new AccountController();
		BLToViewResultMessage message = service.requireRegister(username, password);
		return message;
	}

	@Override
	public BLToViewResultMessage checkUsername(String username)
			throws RemoteException {
		AccountBLService service = new AccountController();
		BLToViewResultMessage message = service.checkUsername(username);
		return message;
	}

	@Override
	public BLToViewResultMessage changePassword(int userID, String newPassword)
			throws UserNotFoundException, RemoteException {
		AccountBLService service = new AccountController();
		BLToViewResultMessage message = service.changePassword(userID, newPassword);
		return message;
	}
	
	public CustomerBasicInfoVO requireMemberInfo(int memberID)
			throws MemberNotFoundException, RemoteException {
		AccountBLService service = new AccountController();
		CustomerBasicInfoVO basicInfoVO = service.requireMemberInfo(memberID);
		return basicInfoVO;
	}

	@Override
	public int requireLogin(String username, String password,
			UserIdentity userIdentity) throws WrongPasswordException,
			UserDeletedException, UserNotExistException, RemoteException {
		LoginBLService service = new LoginController();
		int id = service.requireLogin(username, password, userIdentity);
		return id;
	}

	@Override
	public ArrayList<BookVO> search(int catalogID) throws RemoteException {
		BookSearchByCatalogBLService service = new BookSearchByCatalogController();
		ArrayList<BookVO> list = service.search(catalogID);
		return list;
	}

	@Override
	public ArrayList<BookVO> directSearch(String key) throws RemoteException {
		BookSearchByKeyBLService service = new BookSearchByKeyController();
		ArrayList<BookVO> list = service.directSearch(key);
		return list;
	}

	@Override
	public ArrayList<BookVO> advanceSearch(SearchKeyVO key)
			throws RemoteException {
		BookSearchByKeyBLService service = new BookSearchByKeyController();
		ArrayList<BookVO> list = service.advanceSearch(key);
		return list;
	}

	@Override
	public ArrayList<CatalogVO> getList() throws RemoteException {
		CatalogInquiryBLService service = new CatalogInquiryController();
		ArrayList<CatalogVO> list = service.getCatalogList();
		return list;
	}

	@Override
	public String getName(int catalogID) throws RemoteException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getCatalogID(String name) throws RemoteException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ArrayList<BookVO> viewMyFavor(int memberID)
			throws MemberNotFoundException, RemoteException {
		FavorBLService service = new FavorController();
		ArrayList<BookVO> list = service.viewMyFavor(memberID);
		return list;
	}

	@Override
	public BLToViewResultMessage requireFavor(int memberID, int bookID)
			throws MemberNotFoundException, FavorMaxException,
			FavorRepeatException, RemoteException {
		FavorBLService service = new FavorController();
		BLToViewResultMessage message = service.requireFavor(memberID, bookID);
		return message;
	}

	@Override
	public BLToViewResultMessage cancelFavor(int memberID, int bookID)
			throws MemberNotFoundException, NotFavorException, RemoteException {
		FavorBLService service = new FavorController();
		BLToViewResultMessage message = service.cancelFavor(memberID, bookID);
		return message;
	}

	@Override
	public OrderVO startApplication(int memberID)
			throws MemberNotFoundException, RemoteException {
		OrderApplicationBLService service = new OrderApplicationController();
		OrderVO orderVO = service.startApplication(memberID);
		return orderVO;
	}

	@Override
	public BLToViewResultMessage submitOrder(int memberID, OrderVO order)
			throws MemberNotMatchException, MemberNotFoundException,
			RemoteException {
		OrderApplicationBLService service = new OrderApplicationController();
		BLToViewResultMessage message = service.submitOrder(memberID, order);
		return message;
	}

	@Override
	public ArrayList<OrderVO> viewMyOrder(int memberID)
			throws MemberNotFoundException, RemoteException {
		OrderManagementCustomerBLService service = new OrderManagementCustomerController();
		ArrayList<OrderVO> list = service.viewMyOrder(memberID);
		return list;
	}

	@Override
	public BLToViewResultMessage cancelOrder(int memberID, int orderID)
			throws MemberNotFoundException, OrderNotExistException,
			OrderCancelNotPermittedException, RemoteException {
		OrderManagementCustomerBLService service = new OrderManagementCustomerController();
		BLToViewResultMessage message = service.cancelOrder(memberID, orderID);
		return message;
	}

	@Override
	public BLToViewResultMessage deleteOrder(int memberID, int orderID)
			throws MemberNotFoundException, OrderNotExistException,
			RemoteException {
		OrderManagementCustomerBLService service = new OrderManagementCustomerController();
		BLToViewResultMessage message = service.deleteOrder(memberID, orderID);
		return message;
	}

	@Override
	public ArrayList<SaleItemVO> getTrolley(int memberID)
			throws MemberNotFoundException, RemoteException {
		TrolleyManagementBLService service = new TrolleyManagementController();
		ArrayList<SaleItemVO> list = service.getTrolley(memberID);
		return list;
	}

	@Override
	public BLToViewResultMessage addItem(int memberID, SaleItemVO saleItem)
			throws MemberNotFoundException, ItemNotEnoughException,
			ItemNotFoundException, BookInventoryNotFoundException,
			RemoteException {
		TrolleyManagementBLService service = new TrolleyManagementController();
		BLToViewResultMessage message = service.addItem(memberID, saleItem);
		return message;
	}

	@Override
	public BLToViewResultMessage deleteItem(int memberID, int bookID)
			throws MemberNotFoundException, ItemNotFoundException,
			RemoteException {
		TrolleyManagementBLService service = new TrolleyManagementController();
		BLToViewResultMessage message = service.deleteItem(memberID, bookID);
		return message;
	}

	@Override
	public BLToViewResultMessage modifyQuantity(int memberID, int bookID,
			SaleItemVO saleItem) throws MemberNotFoundException,
			ItemNotFoundException, BookInventoryNotFoundException,
			ItemNotEnoughException, RemoteException {
		TrolleyManagementBLService service = new TrolleyManagementController();
		BLToViewResultMessage message = service.modifyQuantity(memberID, bookID, saleItem);
		return message;
	}

	@Override
	public double getTotalPrice(int memberID) throws MemberNotFoundException,
			RemoteException {
		TrolleyManagementBLService service = new TrolleyManagementController();
		double total = service.getTotalPrice(memberID);
		return total;
	}
}
